﻿using PMSWenLvTing.Model.TongJi;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace PMSWenLvTing.DAL
{
    /// <summary>
    /// 专门做统计的DAL
    /// </summary>
    public class TongJiDAL
    {
        DBHelper sqlhelper = new DBHelper();

        /// <summary>
        /// 获取月报的相关统计数据
        /// </summary>
        /// <returns></returns>
        public DTO_YueBao_TJ Get_YueBao_TJ(string strWhere)
        {
            DTO_YueBao_TJ modelRes = new DTO_YueBao_TJ();
            List<string> liscol = new List<string>();
            liscol.Add("SUM(jiedai_renshu_jingnei)");
            liscol.Add("SUM(jiedai_renshu_jingwai)");
            string sql = string.Format("SELECT {0} FROM yuebao ", string.Join(",", liscol));
            if (!string.IsNullOrEmpty(strWhere))
            {
                sql += " where " + strWhere;
            }
            DataTable dt = sqlhelper.GetDataTable(sql, CommandType.Text, null);
            if (dt == null || dt.Rows.Count <= 0)
            {
                return modelRes;
            }
            if (string.IsNullOrEmpty(dt.Rows[0][0].ToString()))
            {
                modelRes.jiedai_renshu_jingnei = 0;
            }
            else
            {
                modelRes.jiedai_renshu_jingnei = int.Parse(dt.Rows[0][0].ToString());
            }
            if (string.IsNullOrEmpty(dt.Rows[0][1].ToString()))
            {
                modelRes.jiedai_renshu_jingwai = 0;
            }
            else
            {
                modelRes.jiedai_renshu_jingwai = int.Parse(dt.Rows[0][1].ToString());
            }


            return modelRes;
        }

        /// <summary>
        /// 获取某个整型的数据
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>返回一个整型的值</returns>
        public int Get_Int_BySql(string sql)
        {
            object obj = sqlhelper.ExecuteScalar(sql, CommandType.Text, null);
            if (obj == null)
            {
                return 0;
            }
            int res = 0;
            res = int.TryParse(obj.ToString(), out res) ? res : 0;
            return res;
        }


    }
}